package gx.web.controller;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;
import gx.entity.Supplier;
import gx.entity.common.Page;
import gx.service.SupplierService;

@Controller
@RequestMapping("/mng/supp")
public class SupplierController {
	@Autowired
	private SupplierService supplierService;
	
	//查询列表
	@RequestMapping("/list.jhtml")
	public ModelAndView list(@RequestParam(name="size",defaultValue="10")int size,
			@RequestParam(name="number",defaultValue="1")int number){
		ModelAndView mav = new ModelAndView("mng/supp/list");
		Page<Supplier> page =supplierService.findByPager(size, number);
		mav.addObject("page",page);
		return mav;
	}
	
	//添加
		@RequestMapping("/add.jhtml")
		public String add(Supplier supplier){
			supplier.setSn(UUID.randomUUID().toString());
			supplierService.save(supplier);
			return "redirect:/mng/supp/list.jhtml";  //redirect 重定向
		}
		//编辑--获取原数据
		@RequestMapping("/detail.jhtml")
		public ModelAndView detail(Integer id){
			ModelAndView mav =  new ModelAndView("mng/supp/detail");
			Supplier supplier = supplierService.get(id);
			mav.addObject("supp",supplier);
			return mav;
		}
		
		//修改
		@RequestMapping("/edit.jhtml")
		public String edit(String name,String address,String email,Integer code,String contact,String remark,String tel,Integer id){
			Supplier supp = supplierService.get(id);
			supp.setName(name);
			supp.setAddress(address);
			supp.setEmail(email);
			supp.setCode(code);
			supp.setContact(contact);
			supp.setRemark(remark);
			supp.setTel(tel);
			supplierService.update(supp);
			return "redirect:/mng/supp/list.jhtml";
		}
		
		//删除
		@RequestMapping("/delete.jhtml")
		public String delete(Integer id){
			supplierService.delete(id);
			return "redirect:/mng/supp/list.jhtml";
		}
		
		//按供货商名查询
		@RequestMapping("/findByName.jhtml")
		public ModelAndView findByName( @RequestParam(name="size",defaultValue="10")int size,
				@RequestParam(name="number",defaultValue="1")int number,String condition){
			ModelAndView mav = new ModelAndView("mng/supp/list");
			Page<Supplier> page =supplierService.findByName(size, number,condition);
			mav.addObject("page",page);
			return mav;
		}
		

		@RequestMapping(value = "/ajaxdelete.jhtml", produces = "application/json;charset=UTF-8")
		@ResponseBody
		public String ajaxList(Integer id) throws Exception {
			String rs = "";
			try {
				supplierService.delete(id);
				rs = "删除成功！";
			} catch (Exception e) {
				rs = "该供应商在采购订单已使用！";
			}
			// 使用Jackson包来处理json
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(rs);
			return json;
		}
}
